package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.repository.ApUserSearchRepository;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/4/28 1:14
 * @Version 1.0
 */

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {

    @Autowired
    private ApUserSearchRepository userSearchRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    @Override
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        // 判断当前用户是否已经保存了这个关键词的搜索
        Query query = new Query();
        if (userId != null && userId != 0) {
            query.addCriteria(Criteria.where("userId").is(userId));
        } else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        query.addCriteria(Criteria.where("keyword").is(keyword));
        ApUserSearch one = mongoTemplate.findOne(query, ApUserSearch.class);
        if (one == null) {
            // 保存用户的搜索记录
            ApUserSearch userSearch = new ApUserSearch();
            if (userId != null && userId != 0) {
                userSearch.setUserId(userId);
            } else {
                userSearch.setEquipmentId(equipmentId);
            }
            userSearch.setKeyword(keyword);
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
        }


    }



    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        // 查询用户搜索记录,默认返回5条数据
        // 获取当前的用户
        User user = AppThreadLocalUtil.get();
        Integer userId = null;
        if (user != null) {
            userId = user.getUserId();
        }
        Query query = new Query();
        if (userId != null) {
            query.addCriteria(Criteria.where("userId").is(userId));
        } else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        // 按时间排序
        Pageable page = PageRequest.of(0, dto.getSize(), Sort.Direction.DESC, "createdTime");
        query.with(page);

        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {

        List<ApUserSearch> hisList = dto.getHisList();
        ArrayList<String> list = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            list.add(userSearch.getId());
        }

        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(list));
        mongoTemplate.remove(query, ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
